package com.ws.shop.action.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Express;
import com.ws.shop.entity.Order;
import com.ws.shop.service.ExpressService;
import com.ws.shop.service.OrderService;
import com.ws.shop.util.DateUtil;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/express/*")
public class ExpressAction extends BaseAdminAction{
	
	private static Logger log = Logger.getLogger(ExpressAction.class);
	@Autowired
	private ExpressService expressService;
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		String q_name = RequestUtil.getStringParameter(request, "q_name", null);
		String start = RequestUtil.getStringParameter(request, "start", null);
		String end = RequestUtil.getStringParameter(request, "end", null);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		if(start == null){
			start = DateUtil.dateToString(new Date(),DateUtil.YYYY_MM_DD);
		}
		if(end == null){
			end = DateUtil.dateToString(new Date(),DateUtil.YYYY_MM_DD);
		}
		try{
			Page page = expressService.findExpressListPage(pageno,0,q_name,start,end);
			model.addAttribute(Page.PAGE, page);
			model.addAttribute("q_name", q_name);
			model.addAttribute("start", start);
			model.addAttribute("end", end);
			return "express_index";
		}catch(Exception e){
			log.error("查询express列表失败",e);
			model.addAttribute(ERROR_MSG, "系统异常，请稍后再试");
			return ERROR;
		}
	}
	
	public String down(HttpServletRequest request,Model model){
		String name = RequestUtil.getStringParameter(request, "name", null);
		String start = RequestUtil.getStringParameter(request, "start", null);
		String end = RequestUtil.getStringParameter(request, "end", null);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		try{
			Page page = expressService.findExpressListPage(pageno,0,name,start,end);
			//TODO 生成excel表
			
//			$keynames = array(
//					'id'=>'ID',
//					'name'=>'用户名',
//					'goodsname'=>'商品名称',
//					'company'=>'物流公司',
//					'number'=>'物流单号',
//					'addtime'=>'登陆时间'
//				);
//				down_xls($expressdata, $keynames,'物流信息_'.date("Y-m-d"));
			
			return "express_index";
		}catch(Exception e){
			log.error("查询express列表失败",e);
			model.addAttribute(ERROR_MSG, "系统异常，请稍后再试");
			return ERROR;
		}
		
	}
	
	@RequestMapping(value="input.jhtml",method=RequestMethod.GET)
	public String input(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		int oid = RequestUtil.getIntParameter(request, "oid", 0);
		if(id == 0){
			if(oid <= 0){
				model.addAttribute(ERROR_MSG, "指定的订单信息不存在");
				return ERROR;
			}
			Order order = orderService.findOrderById(oid);
			if(order == null){
				model.addAttribute(ERROR_MSG, "指定的订单信息不存在");
				return ERROR;
			}
			if(order.getState() != 1){
				model.addAttribute(ERROR_MSG, "此订单已发货");
				return ERROR;
			}
			model.addAttribute("order", order);
		}else if( id > 0){
			Express express = expressService.findExpressById(id);
			model.addAttribute("express", express);
		}else{
			model.addAttribute(ERROR_MSG, "");
			return ERROR;
		}
		model.addAttribute("id", id);
		return "express_input";
	}
	
	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		String name = RequestUtil.getStringParameter(request, "name", null);
		int aid = RequestUtil.getIntParameter(request, "aid", 0);
		int oid = RequestUtil.getIntParameter(request, "oid", 0);
		String goodsname = RequestUtil.getStringParameter(request, "goodsname", null);
		String company = RequestUtil.getStringParameter(request, "company", null);
		String number = RequestUtil.getStringParameter(request, "number", null);
		Express express = new Express(id,name,aid,oid,goodsname,company,number,null);
		try{
			expressService.saveOrUpdateExpress(express);
			if(id == 0)
				orderService.updateOrderState(oid, 2);
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("操作失败："+express,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试！");
		}
	}
	
	public String del(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id <= 0){
			return this.ajaxJsonErrorMessage(response, "请选择您要删除的对象");
		}
		try{
			Express express = expressService.findExpressById(id);
			if(express == null){
				return this.ajaxJsonErrorMessage(response, "您选择要删除的对象不存在");
			}
			expressService.deleteExpressById(id);
			orderService.updateOrderState(express.getOid(), 1);
			return this.ajaxJsonSuccessMessage(response, "删除成功");
		}catch(Exception e){
			log.error("删除失败id="+id,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
		}
	}
    
}
